﻿namespace ExamV5.ManagSys
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmUpLoadUser : Page
    {
        protected Button butClose;
        protected Button butInput;
        protected DropDownList DDLLevel;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private PublicFun publicFun_0 = new PublicFun();
        protected RadioButtonList RBLNandF;
        protected HtmlInputFile UPUserData;

        protected void butInput_Click(object sender, EventArgs e)
        {
            if (this.ViewState["UserPower"].ToString() != "Y")
            {
                base.Response.Write("<script>alert('用户管理权限未开放！')</script>");
            }
            else
            {
                string fileName = this.UPUserData.PostedFile.FileName;
                if (!(fileName.Trim() != ""))
                {
                    base.Response.Write("<script>alert('文件格式不正确！请选择扩展名为xls或xlsx的文件！')</script>");
                }
                else
                {
                    try
                    {
                        string str24 = fileName.Substring(fileName.LastIndexOf('.'));
                        string str25 = ".XLS.XLSX";
                        if (str25.IndexOf(str24.ToUpper()) >= 0)
                        {
                            if (fileName.IndexOf(@"\", 0) > 0)
                            {
                                int num3 = fileName.LastIndexOf(@"\");
                                fileName = fileName.Substring(num3 + 1);
                                fileName = this.httpCookie_0.Value + str24;
                            }
                            this.UPUserData.PostedFile.SaveAs(base.Server.MapPath("../") + @"\PublicFile\" + fileName);
                            string str22 = base.Server.MapPath("../") + @"\PublicFile\" + fileName;
                            string connectionString = "";
                            if (fileName.ToUpper().IndexOf(".XLSX") == -1)
                            {
                                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + str22 + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
                            }
                            else
                            {
                                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + str22 + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
                            }
                            OleDbConnection connection = new OleDbConnection(connectionString);
                            connection.Open();
                            string str18 = "0";
                            string values = "0";
                            string str5 = "";
                            string str8 = "";
                            string str12 = "";
                            string str17 = "";
                            string str16 = "";
                            string str13 = "";
                            string str11 = "";
                            string str14 = "";
                            string str15 = "";
                            string str10 = "A";
                            string str9 = "";
                            string str2 = "";
                            string str4 = "";
                            string password = "";
                            string str = "";
                            string str3 = "";
                            string str19 = "";
                            SqlConnection connection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                            connection2.Open();
                            SqlTransaction transaction = connection2.BeginTransaction();
                            SqlCommand command2 = new SqlCommand {
                                Connection = connection2,
                                CommandTimeout = 0,
                                Transaction = transaction
                            };
                            try
                            {
                                OleDbCommand command = new OleDbCommand("select * from [sheet1$]", connection);
                                OleDbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                            Label_029C:
                                if (!reader.Read())
                                {
                                    goto Label_0E3C;
                                }
                                if ((reader[0].ToString().Replace(" ", "").Replace("　", "") != "") ? !(reader[4].ToString().Trim().Replace(" ", "").Replace("　", "") != "") : true)
                                {
                                    goto Label_029C;
                                }
                                str2 = reader[0].ToString().Trim().Replace(" ", "").Replace("　", "");
                                if (reader[5].ToString() == null)
                                {
                                    str19 = "1";
                                }
                                else
                                {
                                    str19 = reader[5].ToString();
                                }
                                string[] strArray2 = reader[4].ToString().Trim().Replace(@"\", "＼").Split(new char[] { '＼' });
                                for (int i = 0; i < strArray2.Length; i++)
                                {
                                    if (strArray2[i].Trim().Replace(" ", "") != "")
                                    {
                                        if (i != 0)
                                        {
                                            values = this.publicFun_0.GetValues("select ID from DeptInfo where DeptName='" + strArray2[i].Trim().Replace(" ", "") + "' and UPID=" + str18, "ID");
                                            if (values == "")
                                            {
                                                values = this.publicFun_0.GetSqlID("insert into DeptInfo(DeptName,UPID,SortInfo)values('" + strArray2[i].Trim().Replace(" ", "") + "'," + str18 + ",'" + str19 + "')").ToString();
                                            }
                                            str18 = values;
                                        }
                                        else
                                        {
                                            str18 = this.publicFun_0.GetValues("select ID from DeptInfo where DeptName='" + strArray2[i].Trim().Replace(" ", "") + "' and UPID=0", "ID");
                                            if (str18 == "")
                                            {
                                                str18 = this.publicFun_0.GetSqlID("insert into DeptInfo(DeptName,UPID,SortInfo)values('" + strArray2[i].Trim().Replace(" ", "") + "',0,'1')").ToString();
                                            }
                                            values = str18;
                                        }
                                    }
                                }
                                if (reader[6].ToString() == null)
                                {
                                    str8 = "考生";
                                }
                                else if (reader[6].ToString().Replace(" ", "").Replace("　", "") != "")
                                {
                                    if (this.publicFun_0.GetValues("select ID from SysParmInfo where ParmMemo='Worker' and ParmName='" + reader[6].ToString().Replace(" ", "").Replace("　", "") + "' and ParmValue='" + reader[6].ToString().Replace(" ", "").Replace("　", "") + "'", "ID") == "")
                                    {
                                        this.publicFun_0.ExecuteSql("insert into SysParmInfo(ParmName,ParmValue,ParmMemo)values('" + reader[6].ToString().Replace(" ", "").Replace("　", "") + "','" + reader[6].ToString().Replace(" ", "").Replace("　", "") + "','Worker')");
                                    }
                                    str8 = reader[6].ToString().Replace(" ", "").Replace("　", "");
                                }
                                else
                                {
                                    str8 = "考生";
                                }
                                if (reader[11].ToString() == null)
                                {
                                    str14 = "";
                                }
                                else if (!(reader[11].ToString().Replace(" ", "").Replace("　", "") != ""))
                                {
                                    str14 = "";
                                }
                                else
                                {
                                    if (this.publicFun_0.GetValues("select ID from SysParmInfo where ParmMemo='GWName' and ParmName='" + reader[11].ToString().Trim().Replace(" ", "").Replace("　", "") + "' and ParmValue='" + reader[11].ToString().Trim().Replace(" ", "").Replace("　", "") + "'", "ID") == "")
                                    {
                                        this.publicFun_0.ExecuteSql("insert into SysParmInfo(ParmName,ParmValue,ParmMemo)values('" + reader[11].ToString().Trim().Replace(" ", "").Replace("　", "") + "','" + reader[11].ToString().Trim().Replace(" ", "").Replace("　", "") + "','GWName')");
                                    }
                                    str14 = reader[11].ToString().Replace(" ", "").Replace("　", "");
                                }
                                if (reader[7].ToString() == null)
                                {
                                    str11 = "";
                                }
                                else
                                {
                                    str11 = reader[7].ToString().Trim().Replace(" ", "");
                                }
                                if (reader[3].ToString() == null)
                                {
                                    str5 = "男";
                                }
                                else
                                {
                                    str5 = reader[3].ToString().Trim().Replace(" ", "");
                                }
                                if (reader[13].ToString() == null)
                                {
                                    str13 = "";
                                }
                                else
                                {
                                    try
                                    {
                                        str13 = DateTime.Parse(reader[13].ToString().Trim()).ToShortDateString();
                                    }
                                    catch
                                    {
                                        str13 = "";
                                    }
                                }
                                goto Label_0E22;
                            Label_09DA:
                                str9 = "";
                                goto Label_0A06;
                            Label_09E3:
                                str9 = reader[14].ToString().Trim().Replace(" ", "");
                            Label_0A06:
                                if (reader[15].ToString() != null)
                                {
                                    str3 = reader[15].ToString().Trim().Replace(" ", "");
                                }
                                else
                                {
                                    str3 = "";
                                }
                                if (reader[2].ToString() != null)
                                {
                                    password = reader[2].ToString().Trim();
                                }
                                else
                                {
                                    password = "123";
                                }
                                password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").Trim();
                                if (password.Length > 20)
                                {
                                    password = password.Substring(0, 20);
                                }
                                if (reader[8].ToString() == null)
                                {
                                    str12 = "";
                                }
                                else
                                {
                                    try
                                    {
                                        str12 = DateTime.Parse(reader[8].ToString().Trim()).ToShortDateString();
                                    }
                                    catch
                                    {
                                        str12 = "";
                                    }
                                }
                                if (reader[9].ToString() == null)
                                {
                                    str17 = "";
                                }
                                else
                                {
                                    str17 = reader[9].ToString().Trim().Replace(" ", "");
                                }
                                if (reader[10].ToString() != null)
                                {
                                    str16 = reader[10].ToString().Trim().Replace(" ", "");
                                }
                                else
                                {
                                    str16 = "";
                                }
                                if (reader[12].ToString() != null)
                                {
                                    str15 = reader[12].ToString().Trim().Replace(" ", "");
                                }
                                else
                                {
                                    str15 = "";
                                }
                                if (reader[1].ToString() != null)
                                {
                                    str4 = reader[1].ToString().Trim().Replace(" ", "").Replace("　", "");
                                }
                                else
                                {
                                    str4 = "考生姓名";
                                }
                                if (this.RBLNandF.SelectedItem.Value != "N")
                                {
                                    if (str2.Trim() == "admin")
                                    {
                                        str2 = "ａｄｍｉｎ";
                                    }
                                    str = "delete UserInfo where UserCode='" + str2 + "';delete UserPower where UserCode='" + str2 + "'";
                                }
                                else
                                {
                                    str = "";
                                }
                                str = str + ";Insert into UserInfo(LoginIP,UserCode,UserName,UserSex,UserPwd,DeptID,Worker,UserPhoto,UserLevel,Flag,StyleInfo,HandCombNO,RacingFen,HandMemo,HandImg,UserAge,JoinWorkDate,UserGWName,UserGWLevel,UserSFZH,UserEdu,UserMemo,UserFen,UserMember) values(";
                                str = str + "'" + str3 + "','" + str2 + "','" + str4 + "','" + str5 + "','" + password + "','" + values + "','" + str8 + "','" + str9 + "','" + this.DDLLevel.SelectedItem.Value + "','" + str10 + "','N','" + str11 + "','-1','','','" + str12 + "','" + str13 + "','" + str14 + "','" + str15 + "','" + str16 + "','" + str17 + "','','0','1')";
                                if (!(this.DDLLevel.SelectedItem.Value == "A"))
                                {
                                    str = str + ";insert into UserPower(UserCode,ManagPower,AddNews,AddForum,AddPoll,AddRubric,AddSubject,AddText,AddPaper,ManagUser,ManagExam,AddLession,AddStudyPlan,AddExam) values('" + str2 + "','N','Y','Y','Y','Y','Y','Y','Y','Y','N','Y','Y','Y')";
                                }
                                else
                                {
                                    str = str + ";insert into UserPower(UserCode) values('" + str2 + "')";
                                }
                                command2.CommandText = str;
                                command2.ExecuteNonQuery();
                                goto Label_029C;
                            Label_0E22:
                                if (reader[14].ToString() != null)
                                {
                                    goto Label_09E3;
                                }
                                goto Label_09DA;
                            Label_0E3C:
                                reader.Close();
                                command.Dispose();
                                connection.Close();
                                connection.Dispose();
                                transaction.Commit();
                                base.Response.Write("<script>alert('导入用户名单数据操作成功！');window.opener.location.reload();window.close();</script>");
                            }
                            catch
                            {
                                transaction.Rollback();
                                base.Response.Write("<script>alert('导入用户数据出错！请检查文件格式是否正确或有重复准考证号！" + str2 + "')</script>");
                            }
                            finally
                            {
                                connection2.Close();
                                connection2.Dispose();
                                connection.Close();
                                connection.Dispose();
                            }
                        }
                        else
                        {
                            base.Response.Write("<script>alert('文件格式不正确！请选择扩展名为xls或xlsx的文件')</script>");
                        }
                    }
                    catch
                    {
                        base.Response.Write(@"<script>alert('导入数据失败！\n\n请将PublicFile目录设置为Everyone用户可修改权限！')</script>");
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if (!((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false))
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
            else
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    this.ViewState["UserPower"] = this.publicFun_0.GetValues("select ManagUser from UserPower where UserCode='" + this.httpCookie_0.Value + "'", "ManagUser");
                    if (this.httpCookie_2.Value == "C")
                    {
                        ListItem item = new ListItem("管理员", "C");
                        this.DDLLevel.Items.Add(item);
                    }
                }
            }
        }
    }
}

